﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace FBNotifier
{
    [ComVisible(true)]
    public class JSEventsListener
    {
        public void LinkClicked(string url)
        {
            Process.Start(url);
        }
    }

    /// <summary>
    /// Interaction logic for MainAppWindow.xaml
    /// </summary>
    public partial class MainAppWindow : Window
    {
        JSEventsListener eventListner = new JSEventsListener();

        public MainAppWindow()
        {
            InitializeComponent();

            SetWindowToBottomRightOfScreen();
        }

        private void SetWindowToBottomRightOfScreen()
        {
            Left = SystemParameters.WorkArea.Width - Width;
            Top = SystemParameters.WorkArea.Height - Height;
        }

        private void PinButton_Click(object sender, RoutedEventArgs e)
        {
            if (PinButton.IsChecked == true)
            {
                PinImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/Pinned.png"));
                Topmost = true;
            }
            else
            {
                PinImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/Un-Pinned.png"));
                Topmost = false;
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            CloseButton.IsChecked = false;
            Hide();
        }

        public void ShowAndActivate()
        {
            SetWindowToBottomRightOfScreen();
            Show();
            Activate();

            if ((Application.Current as App).CurrentNotificationsCount == 0 && (Application.Current as App).CurrentMessagesCount > 0)
            {
                tabControl1.SelectedIndex = 0;
            }
            else if ((Application.Current as App).CurrentNotificationsCount > 0 && (Application.Current as App).CurrentMessagesCount == 0)
            {
                tabControl1.SelectedIndex = 1;
            }
            else
            {
                tabControl1.SelectedIndex = 0;
            }
        }

        private void theForm_Initialized(object sender, EventArgs e)
        {
            messagesBrowser.NavigateToString("<html><body style=\"margin:0; padding:0; overflow: auto;\" oncontextmenu=\"return false;\"><div id=\"MyDiv\"></div></body><html>");
            notificationsBrowser.NavigateToString("<html><body style=\"margin:0; padding:0; overflow: auto;\" oncontextmenu=\"return false;\"><div id=\"MyDiv\"></div></body><html>");

            messagesBrowser.ObjectForScripting = eventListner;
            notificationsBrowser.ObjectForScripting = eventListner;
        }

        private static string savedMessagesHtml = "";
        private static string savedNotificationsHtml = "";

        public void RefreshBrowsersContent()
        {
            string messages = (Application.Current as App).CurrentMessagesHtml;
            string notifications = (Application.Current as App).CurrentNotificationsHtml;
            string profileName = (Application.Current as App).CurrentProfileName;

            string title = "FBNotifier : " + profileName;
            if (title.Length > 35)
            {
                title = title.Substring(0, 35) + "...";
            }
            labelTitle.Content = title;

            if ((Application.Current as App).CurrentMessagesCount == 0)
            {
                tabItemMessages.Header = "Messages";

                messagesBrowser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tabItemMessages.Header = String.Format("Messages ({0})", (Application.Current as App).CurrentMessagesCount);
 
                messagesBrowser.Visibility = System.Windows.Visibility.Visible;
            }

            if ((Application.Current as App).CurrentNotificationsCount == 0)
            {
                tabItemNotifications.Header = "Notifications";

                notificationsBrowser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tabItemNotifications.Header = String.Format("Notifications ({0})", (Application.Current as App).CurrentNotificationsCount);

                notificationsBrowser.Visibility = System.Windows.Visibility.Visible;
            }

            mshtml.HTMLDocument messagesDoc = (mshtml.HTMLDocument)messagesBrowser.Document;
            mshtml.HTMLDocument notificationsDoc = (mshtml.HTMLDocument)notificationsBrowser.Document;

            if (messagesDoc != null)
            {
                //if (savedMessagesHtml != messages) this is commented because ot the time ago strings
                {
                    messagesDoc.getElementById("MyDiv").innerHTML = messages;
                    savedMessagesHtml = messages;
                }
            }

            if (notificationsDoc != null)
            {
                //if (savedNotificationsHtml != notifications) this is commented because ot the time ago strings
                {
                    notificationsDoc.getElementById("MyDiv").innerHTML = notifications;
                    savedNotificationsHtml = notifications;
                }
            }
        }

        /*private void msgbtn_Click(object sender, RoutedEventArgs e)
        {
            List<FacebookMessage> messages = new List<FacebookMessage>();
            FacebookMessage message = new FacebookMessage("", "1272718027", "msg " + DateTime.Now, "Това е едно доста яко съобщение че и на няколко реда", "", "Atanas Dimitrov Bakalov");

            messages.Add(message);

            NewEventsSystem.PushNewEvents(messages, new List<FacebookNotification>());
        }

        private void notifbtn_Click(object sender, RoutedEventArgs e)
        {
            List<FacebookNotification> notifications = new List<FacebookNotification>();
            FacebookNotification notification = new FacebookNotification("", "ntf " + DateTime.Now, "Абе 108 човека ти харесаха снимката. Това са , Абе 108 човека ти харесаха снимката. Това са , Абе 108 човека ти харесаха снимката. Това са , Абе 108 човека ти харесаха снимката. Това са", "", "1286131101");

            notifications.Add(notification);

            NewEventsSystem.PushNewEvents(new List<FacebookMessage>(), notifications);
        }*/
    }
}
